package org.darkforest.common.services.impl;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.constant.Constant;
import org.darkforest.common.services.GoodsCommonService;
import org.darkforest.dao.mapper.GoodsCommonMapper;
import org.darkforest.dao.mapper.GoodsImagesMapper;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.dao.mapper.SpecMapper;
import org.darkforest.model.*;
import org.darkforest.vo.GoodsSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Libing on 2015/7/11.
 */
@Service
public class GoodsCommonServiceImpl implements GoodsCommonService {
    @Autowired
    private GoodsCommonMapper goodsCommonMapper;
    @Autowired
    private GoodsImagesMapper goodsImagesMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SpecMapper specMapper;

    /**
     * 商品定时上架
     *
     * @param map
     */
    public void GroundingGoods(Map<String, Object> map) {
        goodsCommonMapper.updateGoodsStatus(map);
    }

    @Override
    public List<GoodsCommon> findGoodsCommonByCondition(GoodsCommon goodsCommon) {
        return goodsCommonMapper.findSimpleGoodsCommonByCond(goodsCommon);
    }

    @Override
    public void soldOutStoreGoods(Map map) {
        goodsCommonMapper.soldOutStoreGoods(map);
    }

    /**
     * 根据主键查询
     *
     * @param goodsCommonid
     * @return
     */
    @Override
    public GoodsCommonWithBLOBs selectById(Integer goodsCommonid) {
        return goodsCommonMapper.getGoodsCommonWithBLOBs(goodsCommonid);
    }

    /**
     * @param gc
     * @return
     */
    @Override
    public int insertBySelective(GoodsCommonWithBLOBs gc) {
        return goodsCommonMapper.insert(gc);
    }

    /**
     * 根据commonid查询goodsCommon 以及 goodsCommon对应的goods
     *
     * @param goodsCommonid
     * @return
     */
    @Override
    public GoodsCommonWithBLOBs getGoodsCommonWithBlobsAndGoodsById(Integer goodsCommonid) {
        return goodsCommonMapper.getGoodsCommonWithBlobsAndGoodsById(goodsCommonid);
    }

    @Override
    public GoodsDetail getGoodsDetail(Integer goodsCommonId) {
        List<GoodsImages> goodsImages = goodsImagesMapper.selectByGoodsId(goodsCommonId);
        GoodsDetail goodsDetail = goodsCommonMapper.getGoodsDetail(goodsCommonId);
        goodsDetail.setGoodsImages(goodsImages);
        Goods param = new Goods();
        param.setGoodsCommonid(goodsCommonId);
        List<Goods> goodsList = goodsMapper.findGoodsByCondition(param);
        goodsDetail.setGoodss(goodsList);
        List<GoodsSku> goodsSkuList = new ArrayList<GoodsSku>();


        List<String> useSelectSpecList = new ArrayList<String>();

        if(goodsList.size()>1){
            for(Goods goods : goodsList){
                GoodsSku goodsSku  = new GoodsSku();
                goodsSku.setInfo(goods.getGoodsSpec());
                goodsSku.setPrice(goods.getGoodsPrice());
                goodsSku.setCode(goods.getGoodsSpec().replace(",", "-"));
                goodsSku.setGoodsId(goods.getGoodsId());
                goodsSkuList.add(goodsSku);


                useSelectSpecList.add(goods.getGoodsSpec());
            }

        }else{
            GoodsSku goodsSku  = new GoodsSku();
            goodsSku.setInfo(Constant.DEAFULT_GOODS_SPEC);
            goodsSku.setPrice(goodsList.get(0).getGoodsPrice());
            goodsSku.setCode(Constant.DEAFULT_GOODS_SPEC);
            goodsSku.setGoodsId(goodsList.get(0).getGoodsId());

            //只有一个规格的时候
            if(StringUtils.isNotBlank(goodsList.get(0).getGoodsSpec()) && !("N;").equals(goodsList.get(0).getGoodsSpec())){
                goodsSku.setInfo(goodsList.get(0).getGoodsSpec());
                goodsSku.setCode(goodsList.get(0).getGoodsSpec());

                useSelectSpecList.add(goodsList.get(0).getGoodsSpec());
            }

            goodsSkuList.add(goodsSku);
        }

        String useSelectSpec = JSON.toJSONString(useSelectSpecList);
        goodsDetail.setGoodsSkus(goodsSkuList);

        List<Spec> specListAfterDeal = new ArrayList<Spec>();

        if(StringUtils.isNotBlank(goodsDetail.getSpecName())&&!goodsDetail.getSpecName().equals("N;")){

            Spec condition = new Spec();
            condition.setClassId(goodsDetail.getGcId());
            condition.setPageSwitch(false);
            List<Spec> specList =  specMapper.findSpecWithSpecValue(condition);

            Iterator<Spec> specIterator = specList.iterator();

            while(specIterator.hasNext()){
                Spec spec = specIterator.next();
                if(goodsDetail.getSpecName().indexOf(spec.getSpName()) == -1){
                    specIterator.remove();
                }else{
                    List<SpecValue> specValues = spec.getSpecValueList();
                    if(specValues != null || specValues.size() < 1){
                        Iterator<SpecValue> iterator = specValues.iterator();
                        while(iterator.hasNext()){
                            SpecValue specValue =iterator.next();
                            if(useSelectSpec.indexOf(specValue.getSpValueName())==-1){
                                iterator.remove();
                            }
                        }
                    }
                    specListAfterDeal.add(spec);
                }
            }

        }else{

            specListAfterDeal.add(new Spec(1, Constant.GOODS_SPEC));
            SpecValue specValue =  new SpecValue(1,Constant.DEAFULT_GOODS_SPEC,1);
            List<SpecValue> specValueList = new ArrayList<SpecValue>();
            specValueList.add(specValue);
            specListAfterDeal.get(0).setSpecValueList(specValueList);


        }
        goodsDetail.setSpecList(specListAfterDeal);

        return goodsDetail;
    }

    @Override
    public List<GoodsCommon> findSimpleGoodsCommonByCond(GoodsCommon param) {
        return goodsCommonMapper.findSimpleGoodsCommonByCond(param);
    }


}
